package ink.yuanchu.picachoframework.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ink.yuanchu.picachoframework.dao.RoleMapper;
import ink.yuanchu.picachoframework.entity.Permissions;
import ink.yuanchu.picachoframework.entity.Role;
import ink.yuanchu.picachoframework.entity.bo.PermissionsAndRoleId;
import ink.yuanchu.picachoframework.entity.bo.RoleAndPermissions;
import ink.yuanchu.picachoframework.service.RoleAssociatedPermissionsService;
import ink.yuanchu.picachoframework.service.RoleService;
import ink.yuanchu.picachoframework.service.biz.RoleAssociatedPermissionsBiz;
import ink.yuanchu.picachoframework.service.bo.RoleBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author ziyuan
 * @Date: 2021/03/20/14:04
 * @Description:
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleAssociatedPermissionsService roleAssociatedPermissionsService;

    /**
     * 根据角色的id查找
     * @param roleId
     * @return
     */
    @Override
    public Role findByRoleId(String roleId) {
        return roleMapper.findByRoleId(roleId);
    }

    @Override
    public Role selectById(String id) {
        return roleMapper.selectById(id);
    }

    @Override
    public List<Role> selectAll() {
        return roleMapper.selectList(null);
    }

    /**
     * 查询所有的角色和它的权限，以一个角色多个权限的关系封装到一起
     * @return
     */
    @Override
    public List<RoleAndPermissions> getRoleAndPermission() {
        List<Role> roles = roleMapper.selectList(null);
        List<RoleAndPermissions> roleAndPermissionsList = new ArrayList<>();
        for (Role role : roles) {
            RoleAndPermissions roleAndPermissions = new RoleAndPermissions();

            String id = role.getId();
            String roleName = role.getRoleName();
            roleAndPermissions.setId(id);
            roleAndPermissions.setRoleName(roleName);

            List<PermissionsAndRoleId> permissionsAndRoleIdList = getPermissionsAndRoleIdList(id);
            roleAndPermissions.setPermissions(permissionsAndRoleIdList);

            roleAndPermissionsList.add(roleAndPermissions);
        }
        return roleAndPermissionsList;
    }

    private List<PermissionsAndRoleId> getPermissionsAndRoleIdList(String id){
        List<PermissionsAndRoleId> permissionsAndRoleIdList = new ArrayList<>();
        List<Permissions> permissionsList = roleAssociatedPermissionsService.findAllPermissionsByRoleId(id);
        for (Permissions permissions : permissionsList) {
            PermissionsAndRoleId permissionsAndRoleId = new PermissionsAndRoleId();
            String id1 = permissions.getId();
            String permissionId = permissions.getPermissionId();
            String permissionName = permissions.getPermissionName();

            permissionsAndRoleId.setId(id1);
            permissionsAndRoleId.setPermissionId(permissionId);
            permissionsAndRoleId.setPermissionName(permissionName);
            permissionsAndRoleId.setRoleId(id);

            permissionsAndRoleIdList.add(permissionsAndRoleId);
        }
        return permissionsAndRoleIdList;
    }
}
